package com.pro.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pro.dao.impl.ProductDaoImpl;
import com.pro.dao.impl.ProductTypeDaoImpl;
import com.pro.entity.NewsEntity;
import com.pro.entity.ProductEntity;
import com.pro.util.BaseServlet;
import com.pro.util.Result;

/**
 * Servlet implementation class NewsAdminServlet
 */
public class ProductAdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    
	ProductDaoImpl pDaoImpl = new ProductDaoImpl();
	ProductTypeDaoImpl pTypeDaoImpl = new ProductTypeDaoImpl();
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	//实现分页 并且根据条件进行查询
	public void productList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Integer page = Integer.parseInt(request.getParameter("page"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));
		
		String pid =request.getParameter("pid");
		System.out.println(pid);
		String showHome =request.getParameter("showHome");
		String beginDate = request.getParameter("beginDate");
		String endDate = request.getParameter("endDate");
		System.out.println(beginDate);
		ArrayList<ProductEntity> aList = pDaoImpl.productAdminList(page,limit,pid,showHome,beginDate,endDate);
		ArrayList<ProductEntity> aList1 = pDaoImpl.productAdminList(null,null,pid,showHome,beginDate,endDate);
		HttpSession session = request.getSession();
		session.setAttribute("productTypeList", pTypeDaoImpl.productType());
		
		PrintWriter out=response.getWriter();
		out.write(com.pro.util.ResultToLayui.toClient(0, "", aList1.size(), aList));
		out.flush();
		out.close();
	}
	
	public void productUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  int id= Integer.parseInt(request.getParameter("productId"));
		  String productName = request.getParameter("productName");
		  String productImg = request.getParameter("productImg");
		  String productTexture = request.getParameter("productTexture");
		  String productPrice = request.getParameter("productPrice");
		  String productPriceRmb = request.getParameter("productPriceRmb");
		  int pid = Integer.parseInt(request.getParameter("pid"));
		  int showHome = Integer.parseInt(request.getParameter("showHome"));
		  Date date = new Date();
		  String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		  int result = pDaoImpl.productUpdate(new ProductEntity(id, createTime, productName, productImg, productTexture, productPrice, productPriceRmb, pid, showHome));
		  
		  PrintWriter out = response.getWriter(); 
		  out.print(Result.toClient("修改", result>0?true:false));
		  out.flush(); 
		  out.close(); 
	}
	
	public void productAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  String productName = request.getParameter("productName");
		  String productImg = request.getParameter("productImg");
		  String productTexture = request.getParameter("productTexture");
		  String productPrice = request.getParameter("productPrice");
		  String productPriceRmb = request.getParameter("productPriceRmb");
		  int pid = Integer.parseInt(request.getParameter("pid"));
		  int showHome = Integer.parseInt(request.getParameter("showHome"));
		  Date date = new Date();
		  String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		  int result = pDaoImpl.productAdd(new ProductEntity(0, createTime, productName, productImg, productTexture, productPrice, productPriceRmb, pid, showHome));
		  
		  PrintWriter out = response.getWriter(); 
		  out.print(Result.toClient("添加", result>0?true:false));
		  out.flush(); 
		  out.close(); 
	}
	
	public void productDelete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		int id= Integer.parseInt(request.getParameter("productId"));
		int result=pDaoImpl.productDelete(id);
		// 我们把数据输出到页面上
		PrintWriter printWriter = response.getWriter();
		printWriter.print(Result.toClient("删除",result>0?true:false));
		printWriter.flush();
		printWriter.close();
      }

}
